package com.fulihui.information.service;

import org.near.dal.sequence.SeqKit;
import org.near.servicesupport.error.Errors;
import org.near.servicesupport.result.ResultBuilder;
import org.near.servicesupport.util.ConditionValidator;
import org.near.toolkit.common.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.fulihui.information.api.DataProcessService;
import com.fulihui.information.request.SpiderDataUploadRequest;
import com.fulihui.information.result.SpiderDataUploadResponse;
import com.fulihui.information.core.zbus.ZbusProducerHandle;

/**
 *
 * Created by LeeSon on 2016/12/25 0025.
 */
@Component("dataProcessService")
public class DataProcessServiceImpl implements DataProcessService {
    @Autowired
    private ZbusProducerHandle zbusProducerHandle;

    @Override
    public SpiderDataUploadResponse spiderDataUpload(SpiderDataUploadRequest request) {
        ConditionValidator.validate(request);
        //生成业务跟踪编号
        request.setTrackNo(String.valueOf(SeqKit.genId()));

        String messageJson = JSON.toJSONString(request);
        Log.info("C->S: " + messageJson);
        boolean success = zbusProducerHandle.commitToMQ(messageJson);

        return success ? ResultBuilder.succ(SpiderDataUploadResponse.class)
            : ResultBuilder.fail(SpiderDataUploadResponse.class, Errors.Commons.SYSTEM_ERROR);
    }
}